<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\facade\Builder;

use app\common\model\Assets;
use think\App;
use think\facade\View;
use think\response\Json;

class Chapter extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Chapter';

    //验证器
    protected string $validate = 'Chapter';

    //关联模型
    protected array $hasOne = [];

    public function index(): string
    {
        $param = $this->request->param();
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '名称', 'modify' => '2,input'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,input', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right','field' => 'status', 'title' => '上架状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['add' => ['data'=>$param['id'] ,'size' => '1200,1200'], 'deleteAll'])
            ->addRightButtons(['edit' => ['size' => '1200,1200'],  'deliver' => ['title' => '编辑课节', 'size' => '800,800', 'url' => url('section/index')->build(), 'event' => '_openNewPage'], 'delete'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭'],
            ])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->addSearch([
                ['title', '输入章节名称 ', 'input', '', '', '', 'like'],
            ])
            ->addExtraMap(['c_id'=>$param['id']])
            ->fetch();
    }


    public function update(): Json
    {
        try {
            $param = $this->request->post();


            return json($this->model::_update($param, $this->getLogsArguments($param)));
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }


    public function add($tpl = 'chapter/add'): string
    {
        $param = $this->request->param();
        View::assign('c_id',$param['data']);
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = 'chapter/edit', $info = ''): string
    {
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }


    public function save($param = [], $model = false): Json
    {
        try {
            $param = $this->request->post();
            return parent::save($param, $model); // TODO: Change the autogenerated stub
        } catch (\Exception $e) {
            dump($e->getMessage());die;

        }
    }


}
